package cn.gson.fengboot.model.service;

import cn.gson.fengboot.model.dao.DdepDao;
import cn.gson.fengboot.model.dao.DdepandmenusDao;
import cn.gson.fengboot.model.dao.DroleDao;
import cn.gson.fengboot.model.mapper.DdepMapper;
import cn.gson.fengboot.model.pojos.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class DdepService {

    @Autowired
    DdepMapper ddepMapper;

    @Autowired
    DdepDao ddepDao;

    @Autowired
    DroleDao droleDao;

    @Autowired
    DdepandmenusDao ddepandmenusDao;

    /*部门无限极入口方法*/
    public List<QdepFromBo> getLeftTrees() throws Exception {
        //获取所有的记录
        List<DdetVo> orgs = this.listAllSysOrg();
        if (null != orgs && orgs.size() > 0) {
            List<QdepFromBo> allOrgs = new ArrayList<QdepFromBo>();
            for (DdetVo org : orgs) {
                QdepFromBo bo = new QdepFromBo();
                bo.setValue(org.getValue());
                bo.setLabel(org.getLabel());
                bo.setDepUpid(org.getDepUpid());
                allOrgs.add(bo);
            }
            return this.getTreeOrgs(allOrgs);
        }
        return null;
    }

    /*获取部门所有组织架构数据*/
    public List<DdetVo> listAllSysOrg() throws Exception {
        return ddepMapper.selectmydep();
    }



    /*岗位无限极入口方法*/
    public List<QroleFromBo> getLeftRoleTrees() throws Exception {
        //获取所有的记录
        List<DroleVo> orgs = this.listAllroleSysOrg();
        if (null != orgs && orgs.size() > 0) {
            List<QroleFromBo> allOrgs = new ArrayList<QroleFromBo>();
            for (DroleVo org : orgs) {
                QroleFromBo bo = new QroleFromBo();
                bo.setValue(org.getValue());
                bo.setLabel(org.getLabel());
                bo.setRoleUpid(org.getRoleUpid());
                bo.setDepId(org.getDepId());
                allOrgs.add(bo);
            }
            return this.getTreeRoleOrgs(allOrgs);
        }
        return null;
    }

    /*获取岗位所有组织架构数据*/
    public List<DroleVo> listAllroleSysOrg() throws Exception {
        return ddepMapper.selectmyroles();
    }


    /*封装组织架构树状结构*/
    public List<QdepFromBo> getTreeOrgs(List<QdepFromBo> allOrgs) throws Exception {
        List<QdepFromBo> listParentRecord = new ArrayList<QdepFromBo>();
        List<QdepFromBo> listNotParentRecord = new ArrayList<QdepFromBo>();
        // 第一步：遍历allOrgs找出所有的根节点和非根节点
        if (allOrgs != null && allOrgs.size() > 0) {
            for (QdepFromBo org : allOrgs) {
                if (null == org.getDepUpid()) {
                    listParentRecord.add(org);
                } else {
                    listNotParentRecord.add(org);
                }
            }
        }
        // 第二步： 递归获取所有子节点
        if (listParentRecord.size() > 0) {
            for (QdepFromBo record : listParentRecord) {
                // 添加所有子级
                record.setChildren(this.getChildOrgs(listNotParentRecord, Long.parseLong(record.getValue()+"")));
            }
        }
        return listParentRecord;
    }

    /* 递归查询子组织*/
    public List<QdepFromBo> getChildOrgs(List<QdepFromBo> childList, Long parentId) throws Exception {
        List<QdepFromBo> listParentOrgs = new ArrayList<QdepFromBo>();
        List<QdepFromBo> listNotParentOrgs = new ArrayList<QdepFromBo>();
        // 遍历childList，找出所有的根节点和非根节点
        if (childList != null && childList.size() > 0) {
            for (QdepFromBo record : childList) {
                // 对比找出父节点
                if (Long.parseLong(record.getDepUpid()+"") == parentId) {
                    listParentOrgs.add(record);
                } else {
                    listNotParentOrgs.add(record);
                }

            }
        }
        // 查询子节点
        if (listParentOrgs.size() > 0) {
            for (QdepFromBo record : listParentOrgs) {
                // 递归查询子节点
                record.setChildren(getChildOrgs(listNotParentOrgs, Long.parseLong(record.getValue()+"")));
            }
        }
        return listParentOrgs;
    }


    /*封装岗位架构树状结构*/
    public List<QroleFromBo> getTreeRoleOrgs(List<QroleFromBo> allOrgs) throws Exception {
        List<QroleFromBo> listParentRecord = new ArrayList<QroleFromBo>();
        List<QroleFromBo> listNotParentRecord = new ArrayList<QroleFromBo>();
        // 第一步：遍历allOrgs找出所有的根节点和非根节点
        if (allOrgs != null && allOrgs.size() > 0) {
            for (QroleFromBo org : allOrgs) {
                if (null == org.getRoleUpid()) {
                    listParentRecord.add(org);
                } else {
                    listNotParentRecord.add(org);
                }
            }
        }
        // 第二步： 递归获取所有子节点
        if (listParentRecord.size() > 0) {
            for (QroleFromBo record : listParentRecord) {
                // 添加所有子级
                record.setChildren(this.getChildRoleOrgs(listNotParentRecord, Long.parseLong(record.getValue()+"")));
            }
        }
        return listParentRecord;
    }

    /* 递归岗位查询子组织*/
    public List<QroleFromBo> getChildRoleOrgs(List<QroleFromBo> childList, Long parentId) throws Exception {
        List<QroleFromBo> listParentOrgs = new ArrayList<QroleFromBo>();
        List<QroleFromBo> listNotParentOrgs = new ArrayList<QroleFromBo>();
        // 遍历childList，找出所有的根节点和非根节点
        if (childList != null && childList.size() > 0) {
            for (QroleFromBo record : childList) {
                // 对比找出父节点
                if (Long.parseLong(record.getRoleUpid()+"") == parentId) {
                    listParentOrgs.add(record);
                } else {
                    listNotParentOrgs.add(record);
                }

            }
        }
        // 查询子节点
        if (listParentOrgs.size() > 0) {
            for (QroleFromBo record : listParentOrgs) {
                // 递归查询子节点
                record.setChildren(getChildRoleOrgs(listNotParentOrgs, Long.parseLong(record.getValue()+"")));
            }
        }
        return listParentOrgs;
    }

    /*新增部门*/
    public void savemydep(QdepFrom qdepFrom) {
        QdepFrom upDep = qdepFrom.getUpDep();//查询新部门的上级部门
        int size = ddepMapper.verifyisempty(upDep.getDepId()).size();//查询是否有其他下级部门
        QdepFrom save = ddepDao.save(qdepFrom);//保存新部门
        if(size<=0){//如果没有，将上级部门的员工转移为新部门（逻辑上，上级部门一般不能有员工）
            List<QroleFrom> verifydepemp = ddepMapper.verifydepemp(upDep.getDepId());
            for (QroleFrom a:verifydepemp) {
                a.setDepId(save);
            }
            droleDao.saveAll(verifydepemp);//修改上级部门员工部门编号
            List<Ddepandmenus> allByDepId = ddepandmenusDao.findAllByDepId(upDep.getDepId());//查询出上级部门的权限组转移给新建部门
            if(allByDepId.size()>0){
                for (Ddepandmenus a:allByDepId){
                    a.setDepId(save.getDepId());
                }
                ddepandmenusDao.saveAll(allByDepId);
            }
        }
    }

    /*判断部门是否可以删除*/
    public Integer verifyisempty(Integer depId){
        int size = ddepMapper.verifyisempty(depId).size();
        if(size>0){
            return 1;//下级有部门
        }else{
            int size1 = ddepMapper.verifydepemp(depId).size();
            if(size1>0){
                return 2;//部门有员工
            }else {
                ddepDao.delete(ddepDao.findById(depId).get());
                return 0;//可删除
            }

        }
    }

    /*根据部门id组查询部门名称*/
    public String selectbymydepid(String deparr) {
        System.out.println(deparr);
        String[] split = deparr.split("/");
        String arr = ddepMapper.selectbymydepid(Integer.parseInt(split[split.length-1])).getLabel();
        /*for(String s:split){
            DdetVo selectbymydepid = ddepMapper.selectbymydepid(Integer.parseInt(s));
            arr = arr +selectbymydepid.getLabel()+"/";
        }

        int index = arr.lastIndexOf("/");//去掉最后一个
        arr =arr.substring(0,index);*/
        return arr;
    }
}
